/////////////////////////////////////////////////////////////////////////////
// Copyright(c) 2012-2015 purang All Rights Reserved
// Name:        socket_proxy.h
// Purpose:
// Author:      syy
// Modified by:
// Created:     2014/8/28 17:32
// RCS-ID:
// Licence:
/////////////////////////////////////////////////////////////////////////////
#ifndef _SOCKET_PROXY_H_
#define _SOCKET_PROXY_H_

#include "socket_proxy_common.h"

namespace qb {
class CSocketProxy {
 public:
  CSocketProxy(void);
  virtual ~CSocketProxy(void);

  static int32_t Test(ProxyType proxy_type, const char* dst_host,
                      uint16_t dst_port, const char* proxy_host,
                      uint16_t proxy_port, const char* username,
                      const char* password, const char* domain);

  static int32_t GetBrowserInfo(std::wstring& strIP, uint16_t& uPort,
                                std::wstring& strUsername,
                                std::wstring& strPassword);

  static void SetGlobalProxy(ProxyType proxy_type, const char* proxy_host,
                             uint16_t proxy_port, const char* username,
                             const char* password, const char* domain);

  void SetProxy(ProxyType proxy_type, const char* proxy_host,
                uint16_t proxy_port, const char* username, const char* password,
                const char* domain);

  udpheader GetUdpHeader(const char* pIPAddress, uint16_t uPort);

  uint16_t GetPort();

  const char* GetIPAddress();

  ProxyType GetProxyType();

  int32_t Connect(uint32_t tSocket, const char* host, uint16_t port,
                  bool use_udp);

  int32_t UdpAssociate(uint32_t tSocket, const char* ipaddress, uint16_t port,
                       std::string& proxy_reply, uint16_t& proxy_reply_port);

 private:
  static void SetProxyInfo(ProxyInfo& info, ProxyType proxy_type,
                           const char* host, uint16_t port,
                           const char* username, const char* password,
                           const char* domain);

  const ProxyInfo& GetInfo();

  int32_t ConnectHTTP(uint32_t tSocket, const char* host, uint16_t port,
                      const char* proxy_host, uint16_t proxy_port,
                      const char* username, const char* password);

  int32_t ConnectSocks5(uint32_t tSocket, const char* host, uint16_t port,
                        const char* proxy_host, uint16_t proxy_port,
                        const char* username, const char* password,
                        std::string remote_addr, uint16_t& remote_proxy_port,
                        bool use_udp);

  int32_t Select(uint32_t tSocket);

#ifdef _MSC_VER
  static bool IsIEVerAbove5();
#endif
 private:
  ProxyInfo m_info;
  ProxyRemoteInfo m_remoteInfo;
  static ProxyInfo g_info;
};
}  // namespace qb

#endif  // _SOCKET_PROXY_H_
